package com.qys.livesMall.shortVideo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.qys.livesMall.framework.view.BaseQuery;
import com.qys.livesMall.shortVideo.mapper.ShortVideoHistoryMapper;
import com.qys.livesMall.shortVideo.model.entity.H2ShortVideo;
import com.qys.livesMall.shortVideo.model.entity.ShortVideoHistory;
import com.qys.livesMall.shortVideo.model.vo.app.AppUserVideoHistoryVO;
import com.qys.livesMall.shortVideo.service.ShortVideoHistoryService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;

/**
 * <p>
 * 用户浏览视频历史记录 服务实现类
 * </p>
 *
 * @author
 * @since 2023-05-30
 */
@Service
public class ShortVideoHistoryServiceImpl extends ServiceImpl<ShortVideoHistoryMapper, ShortVideoHistory> implements ShortVideoHistoryService {

    /**
     * 添加用户历史记录
     *
     * @param video  {@link  H2ShortVideo}
     * @param userId 用户Id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(H2ShortVideo video, Long userId) {
        ShortVideoHistory history = new ShortVideoHistory();
        history.setUserId(userId);
        history.setVideoId(Long.valueOf(video.getId()));
        history.setTitle(video.getTitle());
        history.setUrl(video.getUrl());
        history.setImg(video.getImg());
        history.setDetail(video.getDetail());
        history.setCreateTime(LocalDateTime.now());
        baseMapper.insert(history);
    }

    /**
     * 分页查询用户短视频浏览记录
     *
     * @param baseQuery
     */
    @Override
    public Page<AppUserVideoHistoryVO> page(BaseQuery baseQuery) {
        Page<AppUserVideoHistoryVO> page = new Page<>(baseQuery.getPageNum(), baseQuery.getPageSize());
        page.setRecords(baseMapper.listHistoryToApp(page, baseQuery));
        return page;
    }

    @Override
    public List<Long> watchTodayCollection(Long userId) {
        // 获取今天的日期和时间范围
        LocalDateTime startOfToday = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
        LocalDateTime endOfToday = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);

        LambdaQueryWrapper<ShortVideoHistory> queryWrapper = Wrappers.<ShortVideoHistory>lambdaQuery()
                .select(ShortVideoHistory::getVideoId)
                .eq(ShortVideoHistory::getUserId, userId)
                .between(ShortVideoHistory::getCreateTime, startOfToday, endOfToday);

        List<ShortVideoHistory> videoHistories = baseMapper.selectList(queryWrapper);
        List<Long> collect = Lists.newArrayList();
        if (!CollectionUtils.isEmpty(videoHistories)) {
            collect = videoHistories.stream().map(ShortVideoHistory::getVideoId).toList();
        }
        return collect;
    }
}
